/*
 *  mih_protocol.h
 *  MIHF
 *
 *  Created by injae song on 11. 3. 5..
 *  Copyright 2011 KyungHee Univ. All rights reserved.
 *
 */
#ifndef MIH_PROTOCOL_H
#define MIH_PROTOCOL_H

#include "mih_message.h"
#include "mih_net_sap.h"
#include "mih_dispatcher.h"
#include "mih_event_service.h"
#include "mih_command_service.h"
#include <openssl/rand.h>



typedef struct _mihf_peer {
	
	char *mih_id;
	
	char *addr;
	int port;
	
	struct bufferevent *evb;
	mihf *mih;
	int fd;
	
	unsigned long supported_cmds; // 32
	unsigned long supported_events; // 32bits
	unsigned long long supported_isqueries; // 64bits
	
	mih_transaction_sm *src_transaction_fsm;
	mih_transaction_sm *dst_transaction_fsm;
	
}mihf_peer;

void init_mih_protocol(mihf*);

void mihf_peer_send(mihf_peer *peer, mih_message *msg);

void mih_protocol_handle(mih_message* msg, mihf_peer *peer);

void mih_protocol_msg_in(mih_message* msg, mih_transaction_sm *transaction, mihf_peer *peer);

void mih_protocol_msg_out(mih_message* msg, mih_transaction_sm *transaction, mihf_peer *peer);

mih_transaction_sm *mih_msg_get_transaction(mih_message* msg, mihf *mih);
mihf_peer *get_mihf_peer_by_id(char *mihfid, mihf *mih);
mih_transaction_sm *get_ongoing_transaction_by_tid(unsigned int tid, mihf *mih);
mih_transaction_sm *get_ongoing_dst_transaction_by_tid(unsigned int tid, mihf *mih);
mih_transaction_sm *get_ongoing_src_transaction_by_tid(unsigned int tid, mihf *mih);

mih_transaction_sm *mih_new_dst_transaction(mihf *mih);
mih_transaction_sm *mih_new_src_transaction(mihf *mih);

void mih_protocol_process_indication(mih_message* msg, mih_transaction_sm *sm);
void mih_protocol_process_response(mih_message* msg, mih_transaction_sm *transaction);
void mih_protocol_process_request(mih_message* msg, mih_transaction_sm *sm);

void mih_protocol_transmit(mih_message* msg, void (*cb) (int, mih_message*, void*), void *cb_arg, mihf *mih );

#endif